package com.hzdl.cinema.dao.impl;

import com.hzdl.cinema.dao.UserDao;
import com.hzdl.cinema.domain.User;
import com.hzdl.cinema.util.JDBCUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class UserDaoImpl implements UserDao {
    JdbcTemplate jdbcTemplate = new JdbcTemplate(JDBCUtils.getDataSource());


    @Override
    public void save(User user) {
        String sql = "insert into user(user_Name,user_password,user_Email,user_Status) values (?,?,?,?)";
        jdbcTemplate.update(sql,user.getUserName(),user.getUserPassword(),user.getUserEmail(),user.getUserStatus());

    }

    @Override
    public User finfByUser(String userName) {
        String sql = "select * from user where user_Name=?";
        User user = null;
        try {
            user = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<User>(User.class), userName);
        } catch (DataAccessException e) {
            //e.printStackTrace();
        }
        return user;
    }

    @Override
    public User login(User user) {
        String sql = "select * from user where user_Name=?  and user_password=?" ;
        User userLogin = null;
        try {
            userLogin = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<User>(User.class), user.getUserName(),user.getUserPassword());
        } catch (DataAccessException e) {
           // e.printStackTrace();
        }
        return userLogin;
    }

    @Override
    public User updateUser(String userName) {
        String sql = "select * from user where user_Name=?";
        User user = null;
        try {
             user = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<User>(User.class), userName);
        } catch (DataAccessException e) {
          //  e.printStackTrace();
        }
        return user;
    }

    @Override
    public void updateSave(User user) {
        String sql = "update user set user_password = ?,user_Email=? where user_Name=?";
        jdbcTemplate.update(sql,user.getUserPassword(),user.getUserEmail(),user.getUserName());
    }
    @Override
    public List<User> findAll() {
        String sql = "select * from user";
        List<User> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(User.class));
        return list;
    }

    @Override
    public User findUserById(int userId) {
        String sql = "select * from user where user_Id = ?";
        User user = null;
        try {
            user = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<User>(User.class),userId);
        } catch (DataAccessException e) {
            e.printStackTrace();
        }
        return user;
    }


    @Override
    public void update_users(int userId, String userName, String userPassword, String userEmail) {
        String sql = "update user set user_Name, user_password = ?,user_Email=? where user_Id=?";
        jdbcTemplate.update(sql,userId,userName,userPassword,userEmail);
    }

    @Override
    public User findUser(String userName) {
        String sql = "select * from user where user_Name=?";
        User user = null;
        try {
            user = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(User.class),userName);
        } catch (DataAccessException e) {
            //         e.printStackTrace();
        }
        return user;
    }

    @Override
    public User findByName(String userName) {
        String sql = "select * from user where user_Name = ?";
        User user = null;
        try {
            user = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(User.class),userName);
        } catch (DataAccessException e) {
            e.printStackTrace();
        }
        return user;
    }

    @Override
    public void addUser(String userName, String userPassword, String userEmail, Integer userStatus) {
        String sql = "insert into user values(null,?,?,?,?)";
        jdbcTemplate.update(sql,userName,userPassword,userEmail,userStatus);
    }

    @Override
    public void selectUserById(int user) {
        String sql = "delete from user where user_Id = ? ";
        jdbcTemplate.update(sql,user);
    }

    @Override
    public User searchUserByName(String userName) {
        String sql = "select * from user where user_Name = ?";
        User user = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(User.class), userName);
        return user;
    }

}
